package pl.ydp.components.mathcell.mathcell
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	import flash.geom.Rectangle;
	import mx.events.ResizeEvent;
	import mx.containers.Box;
	import mx.core.Application;
	import flash.display.DisplayObject;
	import flash.geom.Point;
	
	/**
	 * This class represents the canvas used by the Cell. 
	 * This canvas enables the user do draw the handwritten
	 * characters. */
	public class CellCanvas extends Canvas
	{
		public function CellCanvas()
		{
			super();
			
			drawing = false;
			
			borderCanvas = new Canvas();
			borderCanvas.percentWidth = 100;
			borderCanvas.percentHeight = 100;
			addChild(borderCanvas);
			
			spriteUi = new UIComponent();
			spriteUi.percentWidth = 100;
			spriteUi.percentHeight = 100;
			addChild(spriteUi);
			sprite = new Sprite()
			spriteUi.addChild(sprite);
			drawingBeyondArea = false;
			
			borders = new FrameValues();
			
			//addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDown);
			//addEventListener(MouseEvent.MOUSE_UP, this.onMouseUp);			
			//addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOut);			
			
			sizeChanged();
			
			clear();
		}
		
		// --------------- FIELDS ---------------
		
		private var drawing:Boolean;
		
		private var lineColor:uint;
		private var lineThickness:uint;
		
		private var borderCanvas:Canvas;
		private var spriteUi:UIComponent;
		public var sprite:Sprite;
		
		private var drawingBeyondArea:Boolean;
		
		private var borders:FrameValues;
		
		private const DRAW_BEYOND:Boolean = true;
		
		
		// --------------- PROPERTIES --------------- 
			
        override public function set width(value:Number):void{
        	super.width = value;
        }
			
        override public function set height(value:Number):void{
        	super.height = value;
        }
        
		// --------------- PROPERTIES METHODS ---------------
        		
        private function sizeChanged():void{
        	spriteUi.width = width - getStyle("borderThickness")*4-4;
        	spriteUi.height = height - getStyle("borderThickness")*4-4;
        } 
        
		// --------------- EVENT HANDLERS --------------- 
		
		public function startDrawing(x:Number, y:Number):void{
			if (!drawing){
				drawingBeyondArea = false;
				var spriteX:Number = getCoord(x);
				var spriteY:Number = getCoord(y);
				sprite.graphics.moveTo( spriteX, spriteY );
				sprite.graphics.lineTo( spriteX, spriteY+1 );
				drawing = true;
			}
		}
		
		public function stopDrawing(x:Number, y:Number):void{
			if (drawing){
				//sprite.graphics.lineTo(mouseX, mouseY);
				this.removeEventListener(MouseEvent.MOUSE_MOVE, processDrawing);
				drawing = false;
				drawingBeyondArea = false;
			}
			
		}
		
		public function processDrawing(x:Number, y:Number):void{
			if (drawing){
				var spriteX:Number = getCoord(x);
				var spriteY:Number = getCoord(y);
				
				if (DRAW_BEYOND){
					sprite.graphics.lineTo(spriteX, spriteY);					
				} else {				
					var drawingBounds:Rectangle = new Rectangle(borders.left, borders.top, width - borders.left - borders.right, height - borders.top - borders.bottom);					

					if (drawingBounds.contains(spriteX, spriteY)){
						if (drawingBeyondArea){
							sprite.graphics.moveTo(spriteX, spriteY-1);
							drawingBeyondArea = false;
						}
						sprite.graphics.lineTo(spriteX, spriteY);
					} else if (!drawingBeyondArea) {
						drawingBeyondArea = true;
						
						if (spriteX < drawingBounds.left)
							spriteX = drawingBounds.left;
						else if (spriteX > drawingBounds.right)
							spriteX = drawingBounds.right;
						
						if (spriteY < drawingBounds.top)
							spriteY = drawingBounds.top;
						else if (spriteY > drawingBounds.bottom)
							spriteY = drawingBounds.bottom;
						
						sprite.graphics.lineTo(spriteX, spriteY);
					}
				}
			}
		}			
		
		private function getCoord(value:Number):Number{
			return value;
		}
		
		
		// --------------- PUBLIC METHODS ---------------
		
		
		public function setLineStyle(_width:uint, _color:uint):void{
			lineThickness = _width;
			lineColor = _color;
			sprite.graphics.lineStyle(lineThickness,lineColor);
		} 
		
		//-------------------------------------------------------------------
		
		public function setBorderStyle(_color:uint, _style:String, _thickness:uint, _sides:String):void{
				borderCanvas.setStyle("borderColor", _color);
				borderCanvas.setStyle("borderStyle", _style);
				borderCanvas.setStyle("borderThickness", _thickness);
				borderCanvas.setStyle("borderSides",_sides);
				
				borders = new FrameValues();
				
				if (_sides == null  ||  _sides.toLowerCase().indexOf("top") != -1  ||  _sides == ""){
					borders.top = _thickness;
				}
				if (_sides == null  ||  _sides.toLowerCase().indexOf("bottom") != -1  ||  _sides == ""){
					borders.bottom = _thickness;
				}
				if (_sides == null  ||  _sides.toLowerCase().indexOf("right") != -1  ||  _sides == ""){
					borders.right = _thickness;
				}
				if (_sides == null  ||  _sides.toLowerCase().indexOf("left") != -1  ||  _sides == ""){
					borders.left = _thickness;
				}
				
		} 
		//-------------------------------------------------------------------
		
		public function clear():void{
			if (!sprite){
				return;
			}
			sprite.graphics.clear();
			sprite.graphics.lineStyle(lineThickness,lineColor);
		}
				
	}
}